package com.piesat.quake.business.device.controller;

import com.piesat.common.annotation.rest.AnonymousPostMapping;
import com.piesat.common.vo.ResultVO;
import com.piesat.quake.business.device.bis.DeviceCache;
import com.piesat.quake.business.device.model.ConfigurationUpdate;
import com.piesat.quake.business.device.model.DeviceConfigVO;
import com.piesat.quake.business.device.model.DeviceUpdate;
import com.piesat.quake.business.device.model.RealtimeControl;
import com.piesat.quake.business.device.service.DeviceService;
import com.piesat.quake.business.device.service.EventDataService;
import com.piesat.quake.business.station.model.StationInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 地震仪接口
 *
 * @author Gersy
 * @date 2023/2/13
 * @since 1.0
 */
@Api(value = "仪器：设备管理")
@RestController
@RequestMapping("/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceCache deviceCache;
    @Autowired
    private EventDataService eventDataService;

    @ApiOperation(value = "批量升级设备")
    @PostMapping("/upgradeDevice")
    ResultVO upgradeDevice(@RequestBody(required = false) List<String> ids) {
        deviceService.upgradeDevice(ids);
        return ResultVO.success("发送升级命令成功");
    }

    @ApiOperation(value = "修改设备配置")
    @PostMapping("/updateConfig")
    ResultVO updateConfig(@RequestBody ConfigurationUpdate configurationUpdate) {
        deviceService.updateConfig(configurationUpdate);
        return ResultVO.success("更新成功");
    }

    @ApiOperation(value = "获取特征值数据")
    @GetMapping("/getFeatureData")
    ResultVO getFeatureData(/*@RequestParam Date fromTime, */@RequestParam String id) {
//        deviceService.getFeatureData(id, fromTime.getTime());
        deviceService.getFeatureData(id, new Date().getTime());
        return ResultVO.success("更新成功");
    }

    @ApiOperation(value = "获取设备信息",response = DeviceConfigVO.class)
    @GetMapping("/getDeviceConfig")
    ResultVO getDeviceConfig(@RequestParam String id) {
        return ResultVO.success(deviceService.getDeviceConfig(id));
    }

    @ApiOperation(value = "获取公共设备信息")
    @GetMapping("/getRegionDeviceConfig")
    ResultVO<DeviceConfigVO> getRegionDeviceConfig(@RequestParam String regionCode) {
        return ResultVO.success(deviceService.getRegionDeviceConfig(regionCode));
    }

    @AnonymousPostMapping("/onConnected")
    @PostMapping("/onConnected")
    ResultVO onConnected(@RequestBody Map<String, Object> map) {
        Set<Map.Entry<String, Object>> entries = map.entrySet();
        return ResultVO.success();
    }

    @AnonymousPostMapping("/getConfig")
    @PostMapping("/getConfig")
    ResultVO getConfig(@RequestParam String id) {
        deviceService.getConfig(id);
        return ResultVO.success();
    }

    @ApiOperation(value = "获取设备日志信息")
    @AnonymousPostMapping("/getDeviceLog")
    @PostMapping("/getDeviceLog")
    ResultVO getDeviceLog(@RequestParam String id,@RequestParam Byte type) {
        deviceService.getDeviceLog(id,type);
        return ResultVO.success();
    }

    @ApiOperation(value = "设备更新")
    @AnonymousPostMapping("/deviceUpdate")
    @PostMapping("/deviceUpdate")
    ResultVO deviceUpdate(@RequestBody DeviceUpdate deviceUpdate) {
        deviceService.deviceUpdate(deviceUpdate);
        return ResultVO.success();
    }

    @ApiOperation(value = "实时控制：1 实时控制，2 事件ftp传输⑦，3 重启，4 特征值ftp传输⑦，5 特征值mqtt传输；" +
            "子控制命令为1 实时控制、 2 事件ftp传输、 4 特征值ftp传输、 5 特征值mqtt传输时有效。 0为停止传输； 1为启动传输。")
    @AnonymousPostMapping("/realtimeControl")
    @PostMapping("/realtimeControl")
    ResultVO realtimeControl(@RequestBody RealtimeControl realtimeControl) {
        deviceService.realtimeControl(realtimeControl);
        return ResultVO.success();
    }

    @AnonymousPostMapping("/onlineControl")
    @PostMapping("/onlineControl")
    ResultVO onlineControl(@RequestBody RealtimeControl realtimeControl) {
        if (realtimeControl.getStatus()==0) {
            deviceCache.onDisconnected(realtimeControl.getId());
        }else {
            StationInfo stationInfo = new StationInfo();
            stationInfo.setId(realtimeControl.getId());
            stationInfo.setDataTimestamp(System.currentTimeMillis());
            stationInfo.setTemperature(36.7f);
            deviceCache.onConnected(stationInfo);
        }
        return ResultVO.success();
    }

    @ApiOperation(value = "查询最新的count条地震事件", notes = "查询最新的count条地震事件")
    @AnonymousPostMapping("/getEvents")
    @GetMapping(value = "/getEvents")
    public ResultVO getEvents(@RequestParam(value = "count") int count, @RequestParam(value = "id", required = false) String id) {
        return ResultVO.success(eventDataService.getEvents(count, id));
    }

}
